package com.hxd.tabactivityfragment.ui.activity

import android.os.Handler
import android.view.View
import com.hxd.tabactivityfragment.R
import com.hxd.tabactivityfragment.common.MyActivity
import com.hxd.tabactivityfragment.bean.UserBean
import com.hxd.tabactivityfragment.mvp.presenter.LoginPresenter
import com.hxd.tabactivityfragment.mvp.view.ILoginView
import com.hxd.tabactivityfragment.util.L
import com.vondear.rxtool.RxSPTool
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.layout_input.*
import org.jetbrains.anko.startActivity

/**
 *  项目名：    FoodOrigins
 *  包名：      com.hxd.tabactivityfragment.ui.activity
 *  文件名:     LoginActivity
 *  创建者:     HXD
 *  创建时间:   2019/3/17 11:21
 *  描述:       登录界面
 */

class LoginActivity : MyActivity(), ILoginView,View.OnClickListener {

    private val loginPresenter by lazy { LoginPresenter() }

    override fun getTitleBarId(): Int {
        return R.id.tb_title_login
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun initView() {
        super.initView()
        //给控件绑定点击事件
        tv_goRegister_login.setOnClickListener(this)
        btn_login.setOnClickListener(this)
        iv_WeChat_login.setOnClickListener(this)
        iv_QQ_login.setOnClickListener(this)
        iv_Blog_login.setOnClickListener(this)

        loginPresenter.bindView(this)
        loginPresenter.initRemPsw()
    }

    override fun setLoadingText(): String? {
        return "正在登录中..."
    }

    override fun onClick(v: View) {
        when(v.id){
            //跳转注册
            R.id.tv_goRegister_login -> { startActivity<RegisterActivity>()}
            //登录
            R.id.btn_login -> {
                rxDialogShapeLoading!!.show()
                Handler().postDelayed({
                    rxDialogShapeLoading!!.dismiss()
                    loginPresenter.login()
                }, 2000)
            }
            //选择微信登录
            R.id.iv_WeChat_login -> {}
            //选择QQ登录
            R.id.iv_QQ_login -> {}
            //选择新浪微博登录
            R.id.iv_Blog_login -> {}
        }
    }

    override fun getAccount(): String {
        L.i("account: ${et_username_login.text}")
        return et_username_login.text.toString()
    }

    override fun getPassword(): String {
        L.i("account: ${et_password_login.text}")
        return et_password_login.text.toString()
    }

    override fun loginSuccess(user: UserBean, result:String) {
        //保存token值
        RxSPTool.putString(getContext(), "token", user.msg)
        RxSPTool.putString(getContext(), "userInfo", result)
        //实现记住密码
        RxSPTool.putString(getContext(), "account", et_username_login.text.toString())
        RxSPTool.putString(getContext(), "password", et_password_login.text.toString())
        RxSPTool.putBoolean(getContext(), "isCheckbox", checkbox_password_login.isChecked)
        myToast("登录成功")
        startActivity<MainActivity>()
    }

    override fun showNetworkError() {

    }

    override fun getDataFail(msg:String) {
        myToast(msg)
    }

    override fun rememberPassword() {
        //设置记住密码
        val isCheck = RxSPTool.getBoolean(getContext(), "isCheckbox")
        if (isCheck){
            et_username_login.setText(RxSPTool.getString(getContext(),"account"))
            et_password_login.setText(RxSPTool.getString(getContext(),"password"))
            checkbox_password_login.isChecked = isCheck
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        loginPresenter.destoryView()
    }

    override fun getPermissionSuccess() {

    }

    override fun getPermissionFail() {

    }

}